package com.hiwan.unions.view.popupWindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.hiwan.unions.R;
import com.pl.wheelview.WheelView;
import com.yanzhenjie.nohttp.Logger;

import java.util.ArrayList;

/**
 * Created by llg on 2017/7/9.
 */
public class MembershipPop extends PopupWindow {
    private Activity activity;
    private View conentView;
    private boolean string = false;
    public OnTextLinstener cardListener;
    private WheelView loopView1, loopView2;
    private int i1 = 0, i2 = 0;
    private ArrayList<String> arry_years1 = new ArrayList<String>();

    public MembershipPop(final Activity context) {
        this.activity = context;
        DisplayMetrics dm = new DisplayMetrics();
        //获取屏幕信息
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.popwindow_data, null);
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 刷新状态
        this.update();
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        conentView.setFocusable(true);
        conentView.setFocusableInTouchMode(true);
        conentView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
                    dismiss();
                return false;
            }
        });
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1f;
                activity.getWindow().setAttributes(params);
            }
        });

        findView(conentView, R.id.disms).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        findView(conentView, R.id.custom).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (string) {
                    Logger.i(" loopView1 " + loopView1.isScrolling());
                    cardListener.OnText(arry_years1.get(i1), i1);
                    dismiss();
                } else {
                    Logger.i("zzzzzzz" + loopView2.getSelectedText());
                    cardListener.OnText(arry_years1.get(i2), i2);
                    dismiss();
                }

            }
        });
        loopView1 = findView(conentView, R.id.loopView1);
        loopView1.setCyclic(false);
        loopView2 = findView(conentView, R.id.loopView2);
        loopView2.setCyclic(false);
        loopView1.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                i1 = id;
            }

            @Override
            public void selecting(int id, String text) {
            }
        });
        loopView2.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                i2 = id;
            }

            @Override
            public void selecting(int id, String text) {
            }
        });
    }

    public <T extends View> T findView(View view, int viewId) {
        return (T) view.findViewById(viewId);
    }

    public void setOnTextLinstener(OnTextLinstener listener) {
        this.cardListener = listener;
    }

    /**
     * 密码输入完回调接口
     */
    public interface OnTextLinstener {
        /**
         * 加载更多数据回调方法，由组件自身触发
         */
        void OnText(String text, int i);
    }

    private int forsize(ArrayList<String> arry_years, String content) {
        for (int i = 0; i < arry_years.size(); i++) {
            if (arry_years.get(i).equals(content)) {
                return i;
            }
        }
        return 0;
    }

    public void showPopupWindow(View view, ArrayList<String> arry_years, String content) {
        this.arry_years1 = arry_years;
        if (!this.isShowing()) {
            int i = forsize(arry_years, content);
            if (arry_years.size() > 2) {
                i1 = i;
                string = true;
                loopView1.setVisibility(View.VISIBLE);
                loopView2.setVisibility(View.GONE);
                loopView1.setData(arry_years);
                loopView1.setDefault(i);
            } else if (arry_years.size() == 1) {
                i2 = i;
                string = false;
                loopView2.setVisibility(View.VISIBLE);
                loopView1.setVisibility(View.GONE);
                loopView2.setItemNumber(arry_years.size());
                loopView2.setData(arry_years);
                loopView2.setDefault(i);
            } else {
                i2 = i;
                string = false;
                loopView2.setVisibility(View.VISIBLE);
                loopView1.setVisibility(View.GONE);
                loopView2.setItemNumber(3);
                loopView2.setData(arry_years);
                loopView2.setDefault(i);
            }
            //获取屏幕信息
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.alpha = 0.6f; // 0.0-1.0
            activity.getWindow().setAttributes(lp);
            // 以下拉方式显示popupwindow
            this.showAtLocation(view,
                    Gravity.BOTTOM, 0, 0);
        }
    }
}

